package com.example.card;

import java.time.LocalDate;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

public class Util {
    public static int numberDayOfWeekInDays(int dayOfWeek, int startDayOfWeek, int days) {
        int count = 0;
        int star = startDayOfWeek;
        for (int i = 0; i < days; i++) {
            if (star == dayOfWeek) {
                count += 1;
            }
            star = (star + 1) % 7;
        }
        return count;
    }

    public static boolean inPeriod(LocalDate day, LocalDate start, LocalDate end) {
        return day.equals(start) || day.equals(end)
                || (day.isAfter(start) && day.isBefore(end));
    }

    public static void main(String[] args) {
        final int numThreads = 5; // 定义线程数量
        final CountDownLatch latch = new CountDownLatch(numThreads); // 创建 CountDownLatch 实例，设置初始计数器数量为线程数量

        for (int i = 0; i < numThreads; i++) {
            final int threadNum = i;
            new Thread(() -> {
                try {
                    // 模拟线程执行任务
                    Thread.sleep((long) (Math.random() * 1000));
                    System.out.println("Thread " + threadNum + " finished its work.");
                    latch.countDown(); // 当前线程执行完毕，计数器减一，触发下一个线程的执行
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }

        try {
            // 等待所有线程执行完毕
            latch.await();
            System.out.println("All threads have finished their work.");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}